package com.zjhcsoft.zjpr.amovie.util;

import java.io.IOException;
import java.io.OutputStream;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class ExcelUtil {
	private WritableWorkbook workbook;
	
	public void createExcel(OutputStream os) throws IOException{
		workbook = Workbook.createWorkbook(os);
	}
	
	public WritableSheet createSheet(String name,int index){
		WritableSheet ws = workbook.createSheet(name, index);
		return ws;
	}
	
	public void addCell(String sheetName, String... strings)
			throws RowsExceededException, WriteException {
		WritableSheet ws = workbook.getSheet(sheetName);
		if (ws == null)
			ws = createSheet(sheetName, 0);
		int j = ws.getRows();
		for (int i = 0; i < strings.length; i++) {
			Label la = new Label(i, j, strings[i]);
			ws.addCell(la);
		}
		j++;
	}
	
	public void addCell(String sheetName, String[]... strings) throws RowsExceededException, WriteException {
		WritableSheet ws = workbook.getSheet(sheetName);
		if (ws == null)
			ws = createSheet(sheetName, 0);
		int j = ws.getRows();
		for (String[] obj : strings) {
			for (int i = 0; i < obj.length; i++) {
				Label la = new Label(i, j, obj[i]);
				ws.addCell(la);
			}
			j++;
		}
	}

	public void writeAndClose() throws IOException {
		if (workbook != null) {
			workbook.write();
			workbook.close();
		}
	}
}
